myApp.controller('addBrand', ['$scope', '$http', '$brand', function($scope, $http, $brand){
	$scope.brandName = "";
	$scope.brandDescription = "无";
	$scope.addBrand = function() {
		if($scope.brandName === "") {
			alert("请填写需要添加的品牌名称！");
		}else {
			var brandData = {"brand.name": $scope.brandName, "brand.description": $scope.brandDescription};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('addBrand', brandData, postCfg).then(function successCallback(response) {
				alert("已添加成功！");
				$scope.brandName = "";
				$scope.brandDescription = "无";

				var promise = $brand.loadAllBrand();
				promise.then(function(data) {
					$brand.allBrand = data;
				}, function(n) {
					$brand.allBrand = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
	};
}]);

myApp.controller('deleteBrand', ['$scope', '$http', '$brand', function($scope, $http, $brand){
	
	$scope.brandId = "";
	$scope.deleteBrand = function() {
		if ($scope.brandId === "" || $scope.brandId === null) {
			alert("请选择需要删除的品牌！");
		}else {
			var brandData = {"brand.brandId": $scope.brandId};

			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('deleteBrand', brandData, postCfg).then(function successCallback(response) {
				alert("已删除成功！");
				$scope.brandId = "";

				var promise = $brand.loadAllBrand();
				promise.then(function(data) {
					$brand.allBrand = data;
				}, function(n) {
					$brand.allBrand = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);

myApp.controller('modifyBrand', ['$scope', '$http', '$brand', function($scope, $http, $brand){
	
	$scope.brandId = "";
	$scope.brandName = "";
	$scope.brandDescription = "无";
	$scope.modifyBrand = function() {
		if($scope.brandId === "" || $scope.brandId === null) {
			alert("请选择需要修改的品牌！");
		}else if ($scope.brandName === "") {
			alert("请填写品牌名称！");
		}else {
			var brandData = {"brand.brandId": $scope.brandId, "brand.name": $scope.brandName, "brand.description": $scope.brandDescription};
			var postCfg = {
				headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
				transformRequest: function (data) {
					return $.param(data);
				}
			};

			$http.post('modifyBrand', brandData, postCfg).then(function successCallback(response) {
				alert("已修改成功！");
				$scope.brandId = "";
				$scope.brandName = "";
				$scope.brandDescription = "无";

				var promise = $brand.loadAllBrand();
				promise.then(function(data) {
					$brand.allBrand = data;
				}, function(n) {
					$brand.allBrand = [{"name": "数据加载出错！"}];
				});
				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
		
	};
}]);